package main

import (
	"github.com/rs/zerolog/log"
	"shyxy-net/pkg/podman"
)

func main() {
	client, err := podman.New()
	if err != nil {
		log.Error().Err(err).Msg("new podman client failed")
		return
	}
	//client.CreateUbuntuContainer()
	testNvidiaRowImage := "docker.io/nvidia/cuda:11.1.1-base"
	sb := podman.NewSpec(testNvidiaRowImage)

	var labels []podman.Label
	labels = append(labels, podman.Label{
		Tag: podman.NvidiaGPUParent,
		Val: "true",
	}, podman.Label{
		Tag: podman.NvidiaGpuCount,
		Val: "all",
	})
	var envs []podman.Env
	envs = append(envs, podman.EnvNvidiaVisibleDevicesAll, podman.EnvNvidiaDriverCapabilities)
	var annotations []podman.Annotation
	annotations = append(annotations, podman.Annotation{
		Key: "run.oci.keep_original_groups",
		Val: "1",
	})
	sb.WithAnnotations(annotations).
		WithCommand("nvidia-smi").
		WithEnvs(envs).
		WithLabels(labels).
		WithName("gpu-test")
	resp, err := client.CreateContainerWithSpec(sb)
	if err != nil {
		return
	}
	log.Info().Msgf("container id: %s", resp.ID)
	if err := client.StartContainer(resp.ID, nil); err != nil {
		log.Error().Msgf("start container failed:%w", err)
		reports, err := client.RemoveContainer(resp.ID, nil)
		if err != nil {
			log.Error().Msgf("remove container failed:%w", err)
		}
		log.Info().Msgf("remove container reports:%v", reports)
	}

}
